﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XMK.Framework.DataAccess;
using XMK.Framework.Model;
using XMK.Utils;
using System.Data;

namespace XMK.Framework.Data
{
    public class ProcData
    {
        /// <summary>
        /// 数据库连接
        /// </summary>
        protected DbProvider dbProvider;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbProvider"></param>
        public ProcData(DbProvider dbProvider)
        {
            this.dbProvider = dbProvider;
        }
        /// <summary>
        /// 取得所有的存储过程定义
        /// </summary>
        /// <returns></returns>
        List<ProcInfo> GetProcInfos()
        {            
            //新建一个数据库操作帮助类
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, "SELECT ProcID,DbConn,ProcName,ProcType,ListName FROM dbo.TS_Proc");
            //执行存储过程
            return DbManager.GetCacheInfoList<ProcInfo>(cmd,
                delegate(out ProcInfo model, DataRow row)
            {
                model = new ProcInfo();
                model.ProcID = ParseHelper.Parse<int>(row["ProcID"]);
                model.DbConn = ParseHelper.Parse<string>(row["DbConn"]);
                model.ProcName = ParseHelper.Parse<string>(row["ProcName"]);
                model.ProcType = (ProcType)Enum.Parse(typeof(ProcType), ParseHelper.Parse<string>(row["ProcType"]));
                model.ListName = ParseHelper.Parse<string>(row["ListName"]);

            });
        }

        List<ProcColInfo> GetProcColInfos()
        {
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, "SELECT WsID,ProcID,ColName,JsonKey,TypeName FROM dbo.TS_ProcWsCol");
            //执行存储过程
            return DbManager.GetCacheInfoList<ProcColInfo>(cmd,
                delegate(out ProcColInfo model, DataRow row)
                {
                    model = new ProcColInfo();
                    model.WsID = ParseHelper.Parse<string>(row["WsID"]);
                    model.ProcID = ParseHelper.Parse<int>(row["ProcID"]);
                    model.ColName = ParseHelper.Parse<string>(row["ColName"]);
                    model.JsonKey = ParseHelper.Parse<string>(row["JsonKey"]);
                    model.TypeName = ParseHelper.Parse<string>(row["TypeName"]);
                });
        }
        List<ProcParaInfo> GetProcParaInfos()
        {
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, "SELECT WsID,ProcID,JsonKey,ParaName,ParaDefault FROM dbo.TS_ProcWsPara");
            //执行存储过程
            return DbManager.GetCacheInfoList<ProcParaInfo>(cmd,
                delegate(out ProcParaInfo model, DataRow row)
                {
                    model = new ProcParaInfo();
                    model.WsID = ParseHelper.Parse<string>(row["WsID"]);
                    model.ProcID = ParseHelper.Parse<int>(row["ProcID"]);
                    model.ParaName = ParseHelper.Parse<string>(row["ParaName"]);
                    model.JsonKey = ParseHelper.Parse<string>(row["JsonKey"]);
                    model.ParaDefault = ParseHelper.Parse<string>(row["ParaDefault"]);
                });
        }

        List<ProcInfo> GetProcWsInfos()
        {
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, "SELECT WsID,ProcID,ParaKey,ProcOrder FROM dbo.TS_ProcWs Order by ProcOrder");
            //执行存储过程
            return DbManager.GetCacheInfoList<ProcInfo>(cmd,
                delegate(out ProcInfo model, DataRow row)
                {
                    model = new ProcInfo();
                    model.WsID = ParseHelper.Parse<string>(row["WsID"]);
                    model.ProcID = ParseHelper.Parse<int>(row["ProcID"]);
                    model.ParaKey = ParseHelper.Parse<string>(row["ParaKey"]);
                    model.ProcOrder = ParseHelper.Parse<int>(row["ProcOrder"]);
                });
        }




        public Dictionary<string, List<ProcInfo>> GetWsProcInfosDict()
        {
            List<ProcInfo> listpw = this.GetProcWsInfos();
            List<ProcInfo> listp = this.GetProcInfos();
            List<ProcParaInfo> listpp = this.GetProcParaInfos();
            List<ProcColInfo> listpc = this.GetProcColInfos();

            var t = (from p in listpw
                     select p.WsID).Distinct();

            Dictionary<string, List<ProcInfo>> dict = new Dictionary<string, List<ProcInfo>>();

            foreach (string wsid in t)
            {
                List<ProcInfo> list = new List<ProcInfo>();
                // 多个存储过程
                var pw = (from p in listpw where p.WsID == wsid orderby p.ProcOrder select p).ToList();
                ProcInfo pi;
                for(int i =0;i<pw.Count;i++)
                {
                    // ws里面的procinfo
                    pi = pw[i];
                    // proc的定义
                    var _pi = (from p in listp where p.ProcID == pi.ProcID select p).Single();
                    pi.DbConn = _pi.DbConn;
                    pi.ProcName = _pi.ProcName;
                    pi.ProcType = _pi.ProcType;
                    pi.ListName = _pi.ListName;

                    // 返回列
                    pi.Cols = (from pc in listpc where pc.WsID == wsid && pc.ProcID == pi.ProcID select pc).ToList();
                    // 参数
                    pi.Paras = (from pp in listpp where pp.WsID == wsid && pp.ProcID == pi.ProcID select pp).ToList();

                    list.Add(pi);
                }
                dict.Add(wsid, list);

            }
            return dict;        
        }       
    }
}
